/**
 * Created with IntelliJ IDEA.
 * Description:无头双向不循环链表的实现
 * User: 87111
 * Date: 2025-09-24
 * Time: 18:13
 */
public class MyLinkedList {
    static class Node {
        public int val;
        public Node prev;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if(head == null) {
            head = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    };
    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if(head == null) {
            head = node;
        }else {
            Node tmp = head;
            while(tmp.next != null) {
                tmp = tmp.next;
            }
            tmp.next = node;
            node.prev = tmp;
        }
    };
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        Node node = new Node(data);
        if(index < 0 || index > size()) {
            System.out.println("index" + "超出插入范围");
            return;
        }
        //头插法
        if(index == 0) {
            addFirst(data);
            return;
        }
        //尾插法
        if(index == size()) {
            addLast(data);
            return;
        }
        //中间插入
        Node tmp = head;
        while(index != 0) {
            tmp = tmp.next;
            index--;
        }
        node.next = tmp;
        node.prev = tmp.prev;
        tmp.prev.next = node;
        tmp.prev = node;
    };
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node tmp = head;
        while(tmp != null) {
            if(tmp.val == key) {
                return true;
            }
            tmp = tmp.next;
        }
        return false;
    };
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        Node tmp = head;
        while(tmp != null) {
            if(tmp.val == key) {
                //判断是否是第一个节点
                if(tmp == head) {
                    head = head.next;
                    //判断是否只有一个节点
                    if(head != null) {
                        head.prev = null;
                    }
                }else {
                    tmp.prev.next = tmp.next;
                    if(tmp.next != null) {
                        tmp.next.prev = tmp.prev;
                    }
                }
                break;
            }else {
                tmp = tmp.next;
            }
        }
    };
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        Node tmp = head;
        while (tmp != null) {
            if (tmp.val == key) {
                //判断是否是第一个节点
                if (tmp == head) {
                    head = head.next;
                    //判断是否只有一个节点
                    if (head != null) {
                        head.prev = null;
                    }
                } else {
                    tmp.prev.next = tmp.next;
                    if (tmp.next != null) {
                        tmp.next.prev = tmp.prev;
                    }
                }
                tmp = tmp.next;
            } else {
                tmp = tmp.next;
            }
        }
    }
    //得到单链表的长度
    public int size() {
        Node tmp = head;
        int count = 0;
        while(tmp != null) {
            count++;
            tmp = tmp.next;
        }
        return count;
    };
    public void display() {
        Node tmp = head;
        while(tmp != null) {
            System.out.print(tmp.val + " ");
            tmp = tmp.next;
        }
        System.out.println();
    };
    public void clear() {
        while(head != null) {
            Node tmp = head.next;
            head.prev = null;
            head.next = null;
            head = tmp;
        }
    };
}
